<%@ include file="/common/taglibs.jsp"%>

<head>
    <title><fmt:message key="userProfile.title"/></title>
    <meta name="menu" content="UserMenu"/>
</head>

<c:set var="delObject" scope="request"><fmt:message key="userList.user"/></c:set>
<script type="text/javascript">var msgDelConfirm =
   "<fmt:message key="delete.confirm"><fmt:param value="${delObject}"/></fmt:message>";
</script>
<section class="content-header">
    <h1>
        <fmt:message key="userProfile.heading"/>
    </h1>
</section>

<section class="content">
    <%@ include file="/common/messages.jsp" %>
    <c:choose>
        <c:when test="${param.from == 'list'}">
            <p><fmt:message key="userProfile.admin.message"/></p>
        </c:when>
        <c:otherwise>
            <p><fmt:message key="userProfile.message"/></p>
        </c:otherwise>
    </c:choose>
    <div class="row">
        <div class="col-sm-5 col-sm-offset-1">
        <form:form commandName="user" method="post" action="userform" id="userForm" autocomplete="off"
                   cssClass="well" onsubmit="return validateUser(this)">
            <form:hidden path="id"/>
            <form:hidden path="version"/>
            <input type="hidden" name="from" value="<c:out value="${param.from}"/>"/>
            <div class="row">
                <spring:bind path="user.username">
                <div class="col-sm-6 form-group${(not empty status.errorMessage) ? ' has-error' : ''}">
                    </spring:bind>
                    <appfuse:label styleClass="control-label" key="user.username"/>
                    <form:input cssClass="form-control" path="username" id="firstName" maxlength="50"/>
                    <form:errors path="username" cssClass="help-block"/>
                </div>
                <spring:bind path="user.name">
                <div class="col-sm-6 form-group${(not empty status.errorMessage) ? ' has-error' : ''}">
                    </spring:bind>
                    <appfuse:label styleClass="control-label" key="user.name"/>
                    <form:input cssClass="form-control" path="name" id="lastName" maxlength="50" />
                    <form:errors path="name" cssClass="help-block"/>
                </div>
            </div>

            <div class="row">
                <spring:bind path="user.password">
                <div class="col-sm-6 form-group${(not empty status.errorMessage) ? ' has-error' : ''}">
                    </spring:bind>
                    <appfuse:label styleClass="control-label" key="user.password"/>
                    <form:password cssClass="form-control" path="password" id="password" maxlength="50" showPassword="true"/>
                    <form:errors path="password" cssClass="help-block"/>
                </div>
                <spring:bind path="user.passwordHint">
                <div class="col-sm-6 form-group${(not empty status.errorMessage) ? ' has-error' : ''}">
                    </spring:bind>
                    <appfuse:label styleClass="control-label" key="user.passwordHint"/>
                    <form:input cssClass="form-control" path="passwordHint" id="passwordHint"/>
                    <form:errors path="passwordHint" cssClass="help-block"/>
                </div>
            </div>
            <c:if test="${pageContext.request.remoteUser == user.username}">
                <span class="help-block">
                    <a href="<c:url value="/updatePassword" />"><fmt:message key='updatePassword.changePasswordLink'/></a>
                </span>
            </c:if>


            <div class="row">
                <spring:bind path="user.email">
                <div class="col-sm-6 form-group${(not empty status.errorMessage) ? ' has-error' : ''}">
                    </spring:bind>
                    <appfuse:label styleClass="control-label" key="user.email"/>
                    <form:input cssClass="form-control" path="email" id="email"/>
                    <form:errors path="email" cssClass="help-block"/>
                </div>
                <div class="col-sm-6 form-group">
                    <appfuse:label styleClass="control-label" key="user.phoneNumber"/>
                    <form:input cssClass="form-control" path="phoneNumber" id="phoneNumber"/>
                </div>
            </div>
            <c:choose>
                <c:when test="${param.from == 'list' or param.from == 'dealeradmin' or param.from == 'bankadmin' or param.method == 'Add'}">
                    <div class="form-group">
                        <label class="control-label"><fmt:message key="userProfile.accountSettings"/></label>
                        <label class="checkbox-inline">
                            <form:checkbox path="enabled" id="enabled"/>
                            <fmt:message key="user.enabled"/>
                        </label>

                        <label class="checkbox-inline">
                            <form:checkbox path="accountExpired" id="accountExpired"/>
                            <fmt:message key="user.accountExpired"/>
                        </label>

                        <label class="checkbox-inline">
                            <form:checkbox path="accountLocked" id="accountLocked"/>
                            <fmt:message key="user.accountLocked"/>
                        </label>

                        <label class="checkbox-inline">
                            <form:checkbox path="credentialsExpired" id="credentialsExpired"/>
                            <fmt:message key="user.credentialsExpired"/>
                        </label>
                    </div>
                    <c:choose>
                        <c:when test="${param.from == 'dealeradmin'}">
                            <input hidden name="dealerId" value="<c:out value="${param.dealerId}"/>"/>
                            <div class="form-group">
                                <label for="userRoles" class="control-label"><fmt:message key="userProfile.assignRoles"/></label>
                                <select id="userRoles" name="userRoles" multiple="true" class="form-control">
                                    <option value="ROLE_DEALER_USER" selected><fmt:message key="ROLE_DEALER_USER"/></option>
                                </select>
                            </div>
                        </c:when>
                        <c:when test="${param.from == 'bankadmin'}">
                            <input hidden name="bankId" value="<c:out value="${param.bankId}"/>"/>
                            <div class="form-group">
                                <label for="userRoles" class="control-label"><fmt:message key="userProfile.assignRoles"/></label>
                                <select id="userRoles" name="userRoles" multiple="true" class="form-control">
                                    <option value="ROLE_Bank_CREDIT" ${fn:contains(user.roles, 'ROLE_Bank_CREDIT') ? 'selected' : ''}><fmt:message key="ROLE_Bank_CREDIT"/></option>
                                    <option value="ROLE_Bank_USER" ${fn:contains(user.roles, 'ROLE_Bank_USER') ? 'selected' : ''}><fmt:message key="ROLE_Bank_USER"/></option>
                                    <option value="ROLE_Bank_TELLER" ${fn:contains(user.roles, 'ROLE_Bank_TELLER') ? 'selected' : ''}><fmt:message key="ROLE_Bank_TELLER"/></option>
                                    <option value="ROLE_Bank_MANAGER" ${fn:contains(user.roles, 'ROLE_Bank_MANAGER') ? 'selected' : ''}><fmt:message key="ROLE_Bank_MANAGER"/></option>
                                    <option value="ROLE_M_RECEIVE" ${fn:contains(user.roles, 'ROLE_M_RECEIVE') ? 'selected' : ''}><fmt:message key="ROLE_M_RECEIVE"/></option>
                                    <option value="ROLE_M_AUDIT" ${fn:contains(user.roles, 'ROLE_M_AUDIT') ? 'selected' : ''}><fmt:message key="ROLE_M_AUDIT"/></option>
                                    <option value="ROLE_M_PRINT" ${fn:contains(user.roles, 'ROLE_M_PRINT') ? 'selected' : ''}><fmt:message key="ROLE_M_PRINT"/></option>
                                </select>
                            </div>
                        </c:when>
                        <c:when test="${param.from == 'list' or param.method == 'Add'}">
                            <div class="form-group">
                                <label for="userRoles" class="control-label"><fmt:message key="userProfile.assignRoles"/></label>
                                <select id="userRoles" name="userRoles" multiple="true" class="form-control">
                                    <c:forEach items="${availableRoles}" var="role">
                                        <option value="${role.value}" ${fn:contains(user.roles, role.value) ? 'selected' : ''}>${role.label}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </c:when>
                    </c:choose>
                </c:when>
                <c:when test="${not empty user.username}">
                    <div class="form-group">
                        <label class="control-label"><fmt:message key="user.roles"/>:</label>
                        <div class="readonly">
                            <c:forEach var="role" items="${user.roleList}" varStatus="status">
                                <fmt:message key="${role.label}"/>
                                <!--c:out value="${role.label}"/--><c:if test="${!status.last}">,</c:if>
                                <input type="hidden" name="userRoles" value="<c:out value="${role.label}"/>"/>
                            </c:forEach>
                        </div>
                        <form:hidden path="enabled"/>
                        <form:hidden path="accountExpired"/>
                        <form:hidden path="accountLocked"/>
                        <form:hidden path="credentialsExpired"/>
                    </div>
                </c:when>
            </c:choose>
            <div class="form-group">
                <button type="submit" class="btn btn-primary" name="save" onclick="bCancel=false">
                    <i class="icon-ok icon-white"></i> <fmt:message key="button.save"/>
                </button>

                <c:if test="${(param.from == 'list' or param.from == 'dealeradmin' or param.from == 'bankadmin') and param.method != 'Add'}">
                    <button type="submit" class="btn btn-default" name="delete" onclick="bCancel=true;return confirmMessage(msgDelConfirm)">
                        <i class="icon-trash"></i> <fmt:message key="button.delete"/>
                    </button>
                </c:if>

                <button type="submit" class="btn btn-default" name="cancel" onclick="bCancel=true">
                    <i class="icon-remove"></i> <fmt:message key="button.cancel"/>
                </button>
            </div>
        </form:form>
    </div>
        </div>
    </section>


<c:set var="scripts" scope="request">
<script type="text/javascript">
// This is here so we can exclude the selectAll call when roles is hidden
function onFormSubmit(theForm) {
    return validateUser(theForm);
}
</script>
</c:set>

<v:javascript formName="user" staticJavascript="false"/>
<script type="text/javascript" src="<c:url value="/scripts/validator.jsp"/>"></script>

